package com.wisco.web.demo.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.wisco.plarform.action.PlatformAction;
import com.wisco.web.demo.model.User;
import com.wisco.web.demo.service.UserService;
import com.wisco.web.util.MD5Code;

@Controller
public class LoginAction extends PlatformAction {
	private static final long serialVersionUID = 1L;
	@Resource
	private UserService userService;
	String username;
	String password;
	User user = new User();
	public void setUsername(String username){
		this.username = username;
	}
	public void setPassword(String password){
		this.password = password;
	}
	//退出系统
	@Action(value="/loginout",results={@Result(name=SUCCESS,location="/admin/index.jsp")})
	public String loginoutAction(){
		HttpSession session=this.getRequest().getSession();
		User sessionuser=(User)session.getAttribute("user");
		if(sessionuser!=null){
			session.setAttribute("user", null);
		}
		
		return SUCCESS;
	}
	
	@Action(value = "/adminlogin", results = { @Result(name = SUCCESS, location = "/admin/main.jsp"),@Result(name = INPUT, location = "/admin/index.jsp") })
	public String loginAdmin() {
	
		MD5Code  md5Code=new MD5Code();
		HttpSession session=this.getRequest().getSession();
		User sessionuser=(User)session.getAttribute("user");
		if(sessionuser!=null){
			return SUCCESS;
		}else{
			if(!"".equals(password)&&password!=null&&username!=null&!"".equals(username)){
				user = userService.loginUser(username, md5Code.getMD5ofStr(password));
				
				if(user==null){
					logger.debug("logger信息：用户名或密码错误");
					
					this.getRequest().setAttribute("erro", "用户名或密码错误");
					return INPUT;
				}else{
					session.setAttribute("user", user);
					return SUCCESS;
				}
			}else{
				return INPUT;
			}
		}
		
	}
	/*
	 * 完成个人登录接口
	 */
	@SuppressWarnings("unchecked")
	@Action(value = "/login", results = { @Result(name = SUCCESS, location = "/welcome.jsp"),@Result(name = INPUT, location = "/error.jsp") })
	@Override
	public String execute() {
		user = userService.loginUser(username, password);
		ActionContext context = ActionContext.getContext();  
		if(user==null){
			logger.debug("logger信息：用户名或密码错误");
			Map request=(Map)context.get("request");  
			//Map session = context.getSession();  
			//Map application = context.getApplication();  
			request.put("exception", new RuntimeException("密码错误")); 
			//this.getRequest();
			return INPUT;
		}
		getSession().put("user", user);
		logger.debug("logger信息 : " + user.getUsername() + " : " + user.getPassword() + ": " + user.getUserid());
		return SUCCESS;
	}

	@Override
	public Object getViewModel() {
		// TODO Auto-generated method stub
		return null;
	}
}
